package com.zhao.fishingguideserver.mapper;

import com.zhao.fishingguideserver.POJO.Favorite;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 收藏数据访问接口
 */
@Mapper
public interface FavoriteMapper {
    
    /**
     * 添加收藏
     */
    @Insert("insert into favorites(user_id, product_id, create_time) " +
            "values(#{userId}, #{productId}, now())")
    @Options(useGeneratedKeys = true, keyProperty = "favoriteId", keyColumn = "favorite_id")
    int insert(Favorite favorite);
    
    /**
     * 取消收藏
     */
    @Delete("delete from favorites where user_id = #{userId} and product_id = #{productId}")
    int deleteByUserIdAndProductId(@Param("userId") Long userId, @Param("productId") Long productId);
    
    /**
     * 根据收藏ID删除
     */
    @Delete("delete from favorites where favorite_id = #{favoriteId}")
    int deleteByFavoriteId(@Param("favoriteId") Long favoriteId);
    
    /**
     * 查询用户的收藏列表（关联商品信息）
     */
    @Select("select f.favorite_id as favoriteId, f.user_id as userId, f.product_id as productId, f.create_time as createTime, " +
            "p.product_name as productName, p.cover_list as coverList, p.price, p.old_level as oldLevel " +
            "from favorites f " +
            "left join product p on f.product_id = p.product_id " +
            "where f.user_id = #{userId} " +
            "order by f.create_time desc")
    List<Favorite> selectByUserId(@Param("userId") Long userId);
    
    /**
     * 检查是否已收藏
     */
    @Select("select count(*) from favorites where user_id = #{userId} and product_id = #{productId}")
    int checkFavorite(@Param("userId") Long userId, @Param("productId") Long productId);
    
    /**
     * 根据商品ID查询收藏数量
     */
    @Select("select count(*) from favorites where product_id = #{productId}")
    int countByProductId(@Param("productId") Long productId);
    
    /**
     * 根据商品ID删除所有收藏记录（订单创建时调用）
     */
    @Delete("delete from favorites where product_id = #{productId}")
    int deleteByProductId(@Param("productId") Long productId);
}

